#ifndef GPIO_H
#define GPIO_H
/*****
  output 18 0 xxxxx xx
                部件   动作
  input  10+AD  00x  xxxx x
               AD/IO 部件 状态
  AD    001     XXXX X + 2BYTE
        AD标识   忽略  T/P  数值

 二进制        hex
 输出
 0000 00 0x    0x     加水
 0000 01 0x    0(4+x) 加热
 0000 10 0x    0(8+x) 进气
 0000 11 0x    0(C+x) 排气
 0001 00 0x    1x     真空泵
 0001 01 0x    1(4+x) 回气
 0001 10 0x    1(8+x) 开前门
 0001 11 0x    1(C+x) 关前门
 0010 00 0x    2x     前充气
 0010 01 0x    2(4+x) 前排气
 0010 10 0x    2(8+x) 开后门
 0010 11 0x    2(C+x) 关后门
 0011 00 0x    3x     后充气
 0011 01 0x    3(4+x) 后排气
 0011 10 0x    3(8+x) 报警
 0011 11 0x    3(C+x) 运行灯
 0100 00 0x    4x     完成灯
 0100 01 0x    4(4+x)     通信正常指示灯
 0100 10 0x    4(8+x)     备用
 0100 11 0x    4(C+x)     备用


 输入
 0000 000 x    0x          下水位
 0000 001 x    0(2+x)      上水位
 0000 010 x    0(4+x)      泵冷却水
 0000 011 x    0(6+x)      夹层压力
 0000 100 x    0(8+x)      前门开到位
 0000 101 x    0(A+x)      前门关到位
 0000 110 x    0(C+x)      前胶到位
 0000 111 x    0(E+x)      后门开到位
 0001 000 x    1x          后门关到位
 0001 001 x    1(2+x)      后胶到位
 0001 010 x    1(4+x)      备用
 0001 011 x    1(6+x)      备用
 0001 100 x    1(8+x)      备用

 AD

 标识  忽略   数值     HEX
 0001 0000  2B  IO  10XXXX
 0010 0000  2B  温度 20XXXX
 0011 0000  2B  压力 30XXXX


 发送B^B--->接受-->确认(10ms内等待)-->是-->发送下一条指令
 ^                   |
 |                   V
 ------------------- 否(*#06#)

*#6B#
*#2B#
  *****/



#include <sys/types.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <memory.h>
#include <unistd.h>


#define SET_GPIO_OFF(portdat,pin)    *portdat&=0xFFFF-(0x0001<<pin);
#define SET_GPIO_ON(portdat,pin)     *portdat|=(0x0001<<pin);
#define READ_GPIO_PIN(portdat,pin)   (*portdat&(0x0001<<pin))

#define GPA_addr 0x7F008000 //gpio基地址＝GPACON

#define GPCCON_offset 0x040
#define GPCDAT_offset 0x044
#define GPCPUD_offset 0x048

#define GPFCON_offset 0X0A0
#define GPFDAT_offset 0X0A4

#define GPKCON0_offset 0x800
#define GPKCON1_offset 0x804
#define GPKDAT_offset 0x808
#define GPKPUD_offset 0x80C

#define GPLCON0_offset 0x810
#define GPLCON1_offset 0x814
#define GPLDAT_offset 0x818
#define GPLPUD_offset 0x81C

#define GPMCON_offset 0x820
#define GPMDAT_offset 0x824
#define GPMPUD_offset 0x828

#define GPNCON_offset 0x830
#define GPNDAT_offset 0x834
#define GPNPUD_offset 0x838

#define GPPCON_offset 0x160
#define GPPDAT_offset 0x164
#define GPPPUD_offset 0x168

#define GPQCON_offset 0x180
#define GPQDAT_offset 0x184
#define GPQPUD_offset 0x188

#define GPECON_offset 0x080
#define GPEDAT_offset 0x084
#define GPEPUD_offset 0x088

#define GPOCON_offset 0x140
#define GPODAT_offset 0x144
#define GPOPUD_offset 0x148


#define COM_CMD(a,b)              command_queue.enqueue(qMakePair(a,b));

#define water_valve_ON  {monitor_program->set_pin_value(water_valve_pin,true,water_valve_isopen);\
    monitor_program->water_valve_open_timer->start(600000);}
#define water_valve_OFF  {monitor_program->set_pin_value(water_valve_pin,false,water_valve_isopen);\
    monitor_program->water_valve_open_timer->stop();}

#define heating_valve_ON {if(water > low && !inter_pressure_switch_isreached){monitor_program->delay_heat(true);}}

#define heating_valve_OFF  monitor_program->delay_heat(false);
#define air_intake_valve_ON  monitor_program->set_pin_value(air_intake_valve_pin,true,air_intake_valve_isopen);
#define air_intake_valve_OFF monitor_program->set_pin_value(air_intake_valve_pin,false,air_intake_valve_isopen);
#define exhaust_valve_ON   monitor_program->set_pin_value(exhaust_valve_pin,true,exhaust_valve_isopen);
#define exhaust_valve_OFF  monitor_program->set_pin_value(exhaust_valve_pin,false,exhaust_valve_isopen);

#define Vacuum_pump_ON          { monitor_program->set_pin_value(Vacuum_pump_pin,true,Vacuum_pump_isopen);\
    if(whole_machine_parameter.exhaust_valve_multiplex) exhaust_valve_ON \
     monitor_program->start_Vacuum_pump_timer();}
#define Vacuum_pump_OFF        {monitor_program->set_pin_value(Vacuum_pump_pin,false,Vacuum_pump_isopen);\
    if(whole_machine_parameter.exhaust_valve_multiplex) exhaust_valve_OFF monitor_program->stop_Vacuum_pump_timer();}
#define air_supply_valve_ON monitor_program->set_pin_value(air_supply_valve_pin,true,air_supply_valve_isopen);
#define air_supply_valve_OFF monitor_program->set_pin_value(air_supply_valve_pin,false,air_supply_valve_isopen);
/*
#define open_front_door          { stop_close_front_door; monitor_program->set_output_pin(open_front_door_pin,true);}
#define stop_open_front_door      {monitor_program->set_output_pin(open_front_door_pin,false); }
#define close_front_door          { stop_open_front_door;monitor_program->set_output_pin(close_front_door_pin,true); }
#define stop_close_front_door      {monitor_program->set_output_pin(close_front_door_pin,false);}
#define front_door_Sealing_strip_Inflation             {monitor_program->set_output_pin(front_door_Sealing_strip_Inflation_pin,true); }
#define stop_front_door_Sealing_strip_Inflation        {if(PSU<MAX_OPEN_DOOR_PRESSURE) \
    monitor_program->set_output_pin(front_door_Sealing_strip_Inflation_pin,false);COM_CMD(32,false)}//PSU>MIN_OPEN_DOOR_PRESSURE &&
#define front_door_Sealing_strip_Exhaust             { monitor_program->set_output_pin(front_door_Sealing_strip_Exhaust_pin,true); }
#define stop_front_door_Sealing_strip_Exhaust    {if(PSU>MIN_OPEN_DOOR_PRESSURE &&PSU<MAX_OPEN_DOOR_PRESSURE) \
    monitor_program->set_output_pin(front_door_Sealing_strip_Exhaust_pin,false);COM_CMD(36,false)}

#define open_back_door                    {stop_close_back_door; monitor_program->set_output_pin(open_back_door_pin,true);}
#define stop_open_back_door               {monitor_program->set_output_pin(open_back_door_pin,false);}
#define close_back_door                   {stop_open_back_door; monitor_program->set_output_pin(close_back_door_pin,true);}
#define stop_close_back_door              {monitor_program->set_output_pin(close_back_door_pin,false); }
#define back_door_Sealing_strip_Inflation  { monitor_program->set_output_pin(back_door_Sealing_strip_Inflation_pin,true); }//上电为高，跟排气调换
#define stop_back_door_Sealing_strip_Inflation   {monitor_program->set_output_pin(back_door_Sealing_strip_Inflation_pin,false); }
#define back_door_Sealing_strip_Exhaust          { monitor_program->set_output_pin(back_door_Sealing_strip_Exhaust_pin,true); }
#define stop_back_door_Sealing_strip_Exhaust      { monitor_program->set_output_pin(back_door_Sealing_strip_Exhaust_pin,false);}


/***
#define water_valve_ON            {if(!water_valve_isopen) {water_valve_isopen=true;\
    SET_GPIO_ON(GPEDAT,1) COM_CMD(0,true)monitor_program->mytimer->open_overtimer(300);}}
#define water_valve_OFF           {if(water_valve_isopen){water_valve_isopen=false;\
    SET_GPIO_OFF(GPEDAT,1)COM_CMD(0,false)monitor_program->mytimer->close_overtimer();}}
#define heating_valve_ON          {if(!heating_valve_isopen && water > low && !inter_pressure_switch_isreached){\
    heating_valve_isopen=true; SET_GPIO_ON(GPEDAT,2) COM_CMD(4,true)}}
#define heating_valve_OFF         {if(heating_valve_isopen){heating_valve_isopen=false;\
    SET_GPIO_OFF(GPEDAT,2) COM_CMD(4,false)}}

#define air_intake_valve_ON       {if(!air_intake_valve_isopen){air_intake_valve_isopen=true;\
    SET_GPIO_ON(GPEDAT,3) COM_CMD(8,true)}}
#define air_intake_valve_OFF      {if(air_intake_valve_isopen){air_intake_valve_isopen=false;\
    SET_GPIO_OFF(GPEDAT,3) COM_CMD(8,false)}}
#define exhaust_valve_ON         {if(!exhaust_valve_isopen){exhaust_valve_isopen=true;\
    SET_GPIO_ON(GPQDAT,3) COM_CMD(12,true)}}
#define exhaust_valve_OFF         {if(exhaust_valve_isopen){exhaust_valve_isopen=false;\
    SET_GPIO_OFF(GPQDAT,3) COM_CMD(12,false)}}

#define Vacuum_pump_ON          {if(!Vacuum_pump_isopen) {Vacuum_pump_isopen=true;\
    if(whole_machine_parameter.exhaust_valve_multiplex)exhaust_valve_ON COM_CMD(16,true)\
    monitor_program->start_Vacuum_pump_timer();SET_GPIO_ON(GPMDAT,0);}}
#define Vacuum_pump_OFF        {if(Vacuum_pump_isopen) {Vacuum_pump_isopen=false;\
    if(whole_machine_parameter.exhaust_valve_multiplex)exhaust_valve_OFF COM_CMD(16,false)\
    monitor_program->stop_Vacuum_pump_timer();SET_GPIO_OFF(GPMDAT,0);}}

#define air_supply_valve_ON       {if(!air_supply_valve_isopen) {air_supply_valve_isopen=true;\
    SET_GPIO_ON(GPMDAT,1) COM_CMD(20,true)}}
#define air_supply_valve_OFF      {if(air_supply_valve_isopen){air_supply_valve_isopen=false;\
    SET_GPIO_OFF(GPMDAT,1) COM_CMD(20,false)}}

#define open_front_door          { stop_close_front_door; SET_GPIO_ON(GPMDAT,2) COM_CMD(24,true)}
#define stop_open_front_door      {SET_GPIO_OFF(GPMDAT,2) COM_CMD(24,false)}
#define close_front_door          { stop_open_front_door; SET_GPIO_ON(GPMDAT,3) COM_CMD(28,true)}
#define stop_close_front_door      { SET_GPIO_OFF(GPMDAT,3) COM_CMD(28,false)}
#define front_door_Sealing_strip_Inflation             {SET_GPIO_ON(GPMDAT,4) COM_CMD(32,true)}
#define stop_front_door_Sealing_strip_Inflation        {if(PSU>MIN_OPEN_DOOR_PRESSURE &&PSU<MAX_OPEN_DOOR_PRESSURE) SET_GPIO_OFF(GPMDAT,4)COM_CMD(32,false)}
#define front_door_Sealing_strip_Exhaust             {  SET_GPIO_ON(GPMDAT,5) COM_CMD(36,true)}
#define stop_front_door_Sealing_strip_Exhaust    {if(PSU>MIN_OPEN_DOOR_PRESSURE &&PSU<MAX_OPEN_DOOR_PRESSURE) SET_GPIO_OFF(GPMDAT,5)COM_CMD(36,false)}

#define open_back_door                    {stop_close_back_door; SET_GPIO_ON(GPQDAT,1) COM_CMD(40,true)}
#define stop_open_back_door               {SET_GPIO_OFF(GPQDAT,1) COM_CMD(40,false)}
#define close_back_door                   {stop_open_back_door; SET_GPIO_ON(GPQDAT,2) COM_CMD(44,true)}
#define stop_close_back_door              {SET_GPIO_OFF(GPQDAT,2) COM_CMD(44,false)}
#define back_door_Sealing_strip_Inflation  { SET_GPIO_ON(GPEDAT,4) COM_CMD(48,true)}//上电为高，跟排气调换
#define stop_back_door_Sealing_strip_Inflation   { SET_GPIO_OFF(GPEDAT,4) COM_CMD(48,false)}
#define back_door_Sealing_strip_Exhaust          {  SET_GPIO_ON(GPQDAT,4) COM_CMD(52,true)}
#define stop_back_door_Sealing_strip_Exhaust      { SET_GPIO_OFF(GPQDAT,4) COM_CMD(52,false)}

****/

#define LED1_OFF		 SET_GPIO_ON(GPKDAT,4)
#define LED1_ON			 SET_GPIO_OFF(GPKDAT,4)
#define LED2_OFF		 SET_GPIO_ON(GPKDAT,5)
#define LED2_ON			 SET_GPIO_OFF(GPKDAT,5)
#define LED3_OFF		 SET_GPIO_ON(GPKDAT,6)
#define LED3_ON			 SET_GPIO_OFF(GPKDAT,6)
#define LED4_OFF		 SET_GPIO_ON(GPKDAT,7)
#define LED4_ON			 SET_GPIO_OFF(GPKDAT,7)

#define   SPK_ON                {SET_GPIO_ON(GPFDAT,3) COM_CMD(56,true)}// K8_OUT_on
#define   SPK_OFF               {SET_GPIO_OFF(GPFDAT,3) COM_CMD(56,false)}// K8_OUT_off
#define   LEDA_ON               {SET_GPIO_ON(GPFDAT,0) COM_CMD(60,true)}// K12_OUT_on //运行指示灯
#define   LEDA_OFF              {SET_GPIO_OFF(GPFDAT,0) COM_CMD(60,false)}// K12_OUT_off
#define   LEDB_ON               {SET_GPIO_ON(GPFDAT,1) COM_CMD(64,true)}// K13_OUT_on  //完成指示灯
#define   LEDB_OFF              {SET_GPIO_OFF(GPFDAT,1) COM_CMD(64,false)}// K13_OUT_off
#define   LED_TEST_COM          {COM_CMD(68,true)}

//#define water_supply_ON    {if(!water_supply_isopen) {water_supply_isopen=true; SET_GPIO_ON(GPLDAT,1false)}}
//#define water_supply_OFF       {if(water_supply_isopen) {water_supply_isopen=false; SET_GPIO_OFF(GPLDAT,1false)}}

extern volatile unsigned int* GPCCON;
extern volatile unsigned int* GPCDAT;
extern volatile unsigned int* GPCPUD;

extern volatile unsigned int* GPKCON0;
extern volatile unsigned int* GPKCON1;
extern volatile unsigned int* GPKDAT;
extern volatile unsigned int* GPKPUD;

extern volatile unsigned int* GPLCON0;
extern volatile unsigned int* GPLDAT;
extern volatile unsigned int* GPLPUD;

extern volatile unsigned int* GPMCON;
extern volatile unsigned int* GPMDAT;
extern volatile unsigned int* GPMPUD;


extern volatile unsigned int* GPNCON;
extern volatile unsigned int* GPNDAT;
extern volatile unsigned int* GPNPUD;


extern volatile unsigned int* GPLCON1;
extern volatile unsigned int* GPLDAT;
extern volatile unsigned int* GPLPUD;

extern volatile unsigned int* GPPCON;
extern volatile unsigned int* GPPDAT;
extern volatile unsigned int* GPPPUD;

extern volatile unsigned int* GPQCON;
extern volatile unsigned int* GPQDAT;
extern volatile unsigned int* GPQPUD;

extern volatile unsigned int* GPECON;
extern volatile unsigned int* GPEDAT;
extern volatile unsigned int* GPEPUD;

extern volatile unsigned int* GPOCON;
extern volatile unsigned int* GPODAT;
extern volatile unsigned int* GPOPUD;

extern volatile unsigned int* GPFCON;
extern volatile unsigned int* GPFDAT;


  // close(fd);
// munmap(GPA_base,1);//解除映射关系


#endif
